package Hot100;

import java.util.ArrayList;
import java.util.List;

public class 分割回文串 {

    List<List<String>> res = new ArrayList<>();
    List<String> path = new ArrayList<>();
    String s;
    int len;

    public List<List<String>> partition(String s) {
        this.s = s;
        len = s.length();
        backTracking(0);
        return res;
    }

    private void backTracking(int index) {
        if (index == len) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index + 1; i <= len; i++) {
            String substring = s.substring(index, i);
            if (!isPalindrome(substring)) continue;
            path.add(substring);
            backTracking(i);
            path.removeLast();
        }
    }

    private boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) if (s.charAt(left++) != s.charAt(right--)) return false;
        return true;
    }
}
